// pages/institutions/institutions.js
var app = getApp()
import loginFn from '../../utils/login.js'
import urls from '../../utils/urls.js'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		canEdit: false,
		schoolId: 0, //机构id
		courseMain: [], //课程类别
		teacherId: 0, //教师id
		teacherName: '', //教师名字
		tag: [], //教学特点标签
		feature: false, //显示教学特点
		features: '', //教学特点字符串
		years: '', //年龄
		bornYear: '', //年龄
		allpicker: '', //所有选项
		types: ['课外辅导-语文', '课外辅导-数学'], //教师类别
		typeIndex: [0, 0], //默认机构类型
		sex: ['未知', '男', '女'],
		sexIdx: 0, //默认性别
		nation: [], //国籍
		nationIdx: 0, //默认国籍
		regionItem: '全部', //地区顶部设置
		regions: ['广东省', '深圳市', '福田区'], //默认地区
		majorIdx: 0, //默认专业方向
		major: [], //专业方向
		primarySize: 'default', //提交按钮样式
		disabled: false,
		plain: false,
		loading: false,
		btnTxt: '提交',
		btnType: 'primary',
		teacherEduList: [], //教育经历
		eductaionlist: [], //要来显示的教学经历
		teacherJobList: [], //工作经历
		logoimg: '/img/upload.png', //机构logo图片
		instImg: [], //机构图片
		teacherImgId: [], //封面照片
		teacherImages: [], //照片墙
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		var that = this;
		if (options.schoolId) {
			this.setData({
				schoolId: options.schoolId
			})
		};
		if (options.teacherId) { //如果有教师id则是编辑
			this.setData({
				teacherId: options.teacherId
			})
		};

		var arr = [];
		arr.push(app.globalData.allselect.courseMain)
		if (app.globalData.allselect.courseMain[0].sub) {
			arr.push(app.globalData.allselect.courseMain[0].sub)
		} else {
			arr[1] = []
		}
		that.setData({
			allpicker: app.globalData.allselect,
			types: app.globalData.allselect.courseMain, //教师类别
			courseMain: arr, //教师类别
			major: app.globalData.allselect.subject, //专业方向
			nation: app.globalData.allselect.nation, //国籍
			tag: app.globalData.allselect.label //教学特点标签
		})
		//编辑
		if (options.teacherId) {
			this.editFn(options)
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		wx.pageScrollTo({
			scrollTop: 0,
			duration: 300
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	// 函数
	bindTypeChange: function(e) { //选择教师类型
		console.log('picker account 发生选择改变，携带值为', e);
		this.setData({
			typeIndex: e.detail.value
		})
	},
	bindcolumnchange: function(e) { //改变课程类别选项
		if (e.detail.column == '0') {
			var all = this.data.courseMain;
			if (this.data.courseMain[0][e.detail.value].sub) {
				all[1] = this.data.courseMain[0][e.detail.value].sub;
			} else {
				all[1] = []
			};
			this.setData({
				courseMain: all
			})
		};
	},
	bindmajorChange: function(e) { //教学方向
		this.setData({
			majorIdx: e.detail.value
		})
	},
	bindsexChange: function(e) { //性别选择
		this.setData({
			sexIdx: e.detail.value
		})
	},
	chooseinstImg: function(e) { //照片墻
		var that = this;
		wx.chooseImage({
			count: 9,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				// tempFilePath可以作为img标签的src属性显示图片
				var tempFilePaths = res.tempFilePaths
				for (var i = 0; i < tempFilePaths.length; i++) {
					that.uploadFn(tempFilePaths[i])
				}
			}
		})
	},
	uploadFn(tempFilePaths) {
		var that = this;
		app.uploadImage(tempFilePaths, (res) => {
			if(res.code == 1) {
				var teacherImages = that.data.teacherImages;
				teacherImages.push(res.data.imageFileId)
				var insImgs = that.data.instImg;
				insImgs.push(tempFilePaths)
				that.setData({
					instImg: insImgs,
					teacherImages: teacherImages
				})
			} else {
				app.alert(res.msg);
			}
		})
	},
	chooselogoImg: function(e) { //封面照片
		var that = this;
		wx.chooseImage({
			count: 1,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				var tempFilePaths = res.tempFilePaths;
				app.uploadImage(tempFilePaths[0], (res) => {
					if(res.code == 1) {
						that.setData({
							logoimg: tempFilePaths[0],
							teacherImgId: res.data.imageFileId
						})
					} else {
						app.alert(res.msg);
					}
				})
			}
		})
	},
	bindRegionChange(e) { //选择地区
		this.setData({
			region: e.detail.value
		})
	},
	sexChange: function(e) { //选择性别
		this.setData({
			sexIdx: e.detail.value
		})
	},
	nationChange: function(e) { //选择性别
		this.setData({
			nationIdx: e.detail.value
		})
	},
	bindRegionChange: function(e) { //选择性别
		this.setData({
			regions: e.detail.value
		})
	},
	featureChange: function(e) { //选择教学特点
		this.setData({
			feature: true
		})
	},
	featureFn: function(e) { //选中返回星期
		console.log('返回')
		if (e.detail) {
			var tag = this.data.tag
			//去除原来选项
			for (var j = 0; j < tag.length; j++) {
				tag[j].checked = false;
			}
			var arr = ''
			for (var i = 0; i < e.detail.length; i++) {
				for (var k = 0; k < tag.length; k++) {
					if (e.detail[i] == tag[k].labelId) {
						tag[k].checked = true;
						if (i == e.detail.length - 1) {
							arr += tag[k].labelName;

						} else {
							arr += tag[k].labelName + '、';
						};
					};
				}
			}
			this.setData({
				features: arr,
				feature: false,
				tag: tag
			})
		} else {
			this.setData({
				feature: false
			})
		}

	},
	closeimg: function(e) { //删除图片
		console.log(e)
		var insImgs = this.data.instImg
		var teacherImages = this.data.teacherImages
		insImgs.splice(e.currentTarget.dataset.idx, 1)
		teacherImages.splice(e.currentTarget.dataset.idx, 1)
		this.setData({
			instImg: insImgs,
			teacherImages: teacherImages
		})
	},
	editEduFn: function(e) { //修改教育经历
		console.log(e)
		var that = this;
		wx.navigateTo({
			url: '/pages/education/index?idx=' + e.currentTarget.dataset.idx,
		})
	},
	canEdit: function(e) { //编辑
		this.setData({
			canEdit: false,
			disabled: false
		})
	},
	editWorkFn: function(e) { //修改教育经历
		console.log(e)
		var that = this;
		wx.navigateTo({
			url: '/pages/experience/index?idx=' + e.currentTarget.dataset.idx,
		})
	},
	formSubmit(e) {
		var that = this;
		this.setData({
			disabled: true
		})
		var datas = e.detail.value
		var saves = true;
		var loginInfo = loginFn();
		for (var key in loginInfo) {
			datas[key] = loginInfo[key]
		}
		datas.teacherEduList = this.data.teacherEduList;
		datas.teacherJobList = this.data.teacherJobList;
		datas.teacherImgId = this.data.teacherImgId;
		datas.teacherImages = this.data.teacherImages;
		for (var key in datas) {
			if (key == 'teacherName' || key == 'teacherImgId' || key == 'teacherImages' || key == 'years') {
				if (datas[key] == "" || datas[key].length == 0) {
					saves = false;
					this.setData({
						disabled: false
					})
					console.log(key)
					wx.showToast({
						title: '必填字段不能为空',
						icon: 'none'
					})
					break
				};
			};
		}
		if (saves) {
			console.log(datas)
			datas.courseMain = that.data.types[this.data.typeIndex[0]].courseMainId
			if (that.data.types[this.data.typeIndex[0]].sub) {
				datas.courseSub = that.data.types[this.data.typeIndex[0]].sub[this.data.typeIndex[1]].courseSubId
			};
			// datas.subjectId = that.data.allpicker.subject[datas.subjectId].subjectId
			datas.nationId = that.data.allpicker.nation[datas.nationId].nationId
			console.log(datas)

			var date = new Date;
			var year = date.getFullYear();
			var nowyear = datas.bornYear
			datas.bornYear = year - nowyear
			var savetag = this.data.tag;
			var tagArr = []
			for (var i = 0; i < savetag.length; i++) {
				if (savetag[i]['checked']) {
					delete savetag[i]['checked']
					tagArr.push(savetag[i])
				};

			}
			console.log(tagArr)
			datas['teacherLabelList'] = tagArr

			if (this.data.teacherId) {
				datas['teacherId'] = this.data.teacherId
			} else {
				datas['schoolId'] = this.data.schoolId
			};
			console.log(datas)

			wx.request({
				url: urls.saveTeacher,
				method: 'POST',
				data: datas,
				success: function(res) {
					console.log(res)
					if (res.data.code == '0') {
						wx.showToast({
							title: '提交成功',
						});
						that.setData({
							canEdit: true
						})
					} else {
						that.setData({
							disabled: false
						})
						wx.showToast({
							title: res.data.info,
						})
					}
				}
			})
		};

	},
	//编辑
	editFn: function(options) {
		var that = this
		var loginInfo = loginFn()
		loginInfo['teacherId'] = options.teacherId
		wx.request({
			url: urls.teacherUrl,
			data: loginInfo,
			success: function(res) {
				console.log(res)
				var teacherInfo = res.data.data
				if (res.data.code == '0') {
					var typeIndex = [0, 0]; //教师类型
					var instImg = []; //照片墙
					var teacherImages = [] //照片墙ID
					var majorIdx = 0; //教学方向
					var bornYear = 0; //年龄
					var nationIdx = 0; //国籍
					var tags = teacherInfo.teacherLabel
					var features = ''; //教学特点
					var eductaionlist = teacherInfo.teacherEdu
					var arr = [];
					arr.push(app.globalData.allselect.courseMain)
					arr[1] = []

					for (var i = 0; i < that.data.types.length; i++) {
						if (that.data.types[i].courseMainId == teacherInfo.courseMain) {
							typeIndex[0] = i
							if (that.data.types[i].sub) {
								for (var j = 0; j < that.data.types[i].sub.length; j++) {
									if (that.data.types[i].sub[j].courseSubId == teacherInfo.courseSub) {
										typeIndex[1] = j
										arr[1] = that.data.types[i].sub
									};
								}
							};
						};
					}
					for (var i = 0; i < teacherInfo.teacherImages.length; i++) {
						teacherImages.push(teacherInfo.teacherImages[i].imageFileId)
						instImg.push(teacherInfo.teacherImages[i].img)
					}
					// for (var i = 0; i < that.data.major.length; i++) {
					//   if (that.data.major[i].subjectId == teacherInfo.subjectId) {
					//     majorIdx = i
					//   };
					// }
					for (var i = 0; i < that.data.nation.length; i++) {
						if (that.data.nation[i].nationId == teacherInfo.nationId) {
							nationIdx = i
						};
					}
					console.log(tags)

					for (var i = 0; i < that.data.tag.length; i++) {
						for (var j = 0; j < tags.length; j++) {
							if (that.data.tag[i].labelId == tags[j].labelId) {
								that.data.tag[i]['checked'] = true
								console.log(that.data.tag.length)
								if (j == tags.length - 1) {
									features += tags[j].labelName
								} else {
									features += tags[j].labelName + '、'
								}
							};
						}
					}
					for (var i = 0; i < eductaionlist.length; i++) {
						for (var k = 0; k < that.data.allpicker.level.length; k++) {
							if (that.data.allpicker.level[k].levelId == eductaionlist[i].levelId) {
								eductaionlist[i]['level'] = that.data.allpicker.level[k].level
							};
						}
					}
					console.log(eductaionlist)
					var date = new Date;
					var year = date.getFullYear();
					bornYear = year - teacherInfo.bornYear;
					that.setData({
						teacherName: teacherInfo.teacherName,
						courseMain: arr,
						typeIndex: typeIndex,
						logoimg: teacherInfo.uploadImg,
						teacherImgId: teacherInfo.teacherImgId,
						instImg: instImg,
						teacherImages: teacherImages,
						years: teacherInfo.years,
						// majorIdx: majorIdx,
						bornYear: bornYear,
						sexIdx: teacherInfo.sex,
						nationIdx: nationIdx,
						regions: teacherInfo.hometown,
						teacherJobList: teacherInfo.teacherJob,
						tag: that.data.tag,
						features: features,
						teacherEduList: teacherInfo.teacherEdu,
						eductaionlist: eductaionlist,
						canEdit: true
					})
				};
			}
		})

	}
})
